import axios from '@/axios.config'

export default {
    state: {
        msg: '',
        info: {},
        userList: [],
        userAllList: [],
        totleNum: 0
    },
    mutations: {
        send_msg(state, payload) {
            state.msg = payload._msg
        },
        add_info(state, payload) {
            state.info = Object.assign({},payload.com_info)
        },
        add_userList(state, payload) {
            state.userList = [].concat(payload.com_userlist)
            state.totleNum = payload.com_total
        },
        add_allUserList(state, payload) {
            state.userAllList = [].concat(payload.com_useralllist)
        }
    },
    actions: {
        //更新用户集成
        updateIntegration({ commit }, payload) {
            return axios.post('/api/user/integration/update',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "更新成功"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) => {
                    console.log(error);
                })
        },
        //用户集成信息
        getIntegrationInfo({ commit }, payload) {
            return axios.post('/api/user/integration/info',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_info', {
                            com_info: res.data.data
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) => {
                    console.log(error);
                })
        },
        //修改密码
        updatePassword({ commit }, payload) {
            return axios.post('/api/auth/user/updatePwd',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "修改密码成功！"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) => {
                    console.log(error);
                })
        },
        //重置密码
        resetPassword({ commit }, payload) {
            return axios.post('/api/auth/user/resetPwd',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "重置密码成功"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) => {
                    console.log(error);
                })
        },
        //添加用户
        createUser({ commit }, payload) {
            return axios.post('/api/auth/user/save', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "保存成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //用户列表分页
        getUserList({ commit }, payload) {
            return axios.post('/api/auth/user/list',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_userList',{
                            com_userlist: res.data.data.t,
                            com_total: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //用户列表所有
        getAllUserList({ commit }, payload) {
            return axios.post('/api/auth/user/list',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_allUserList',{
                            com_useralllist: res.data.data,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //删除用户
        deleteUser({ commit }, payload) {
            return axios.post('/api/auth/user/del',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "删除成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //用户开启
        openUser({ commit }, payload) {
            return axios.post('/api/auth/user/open',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "开启成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //用户关闭
        closeUser({ commit }, payload) {
            return axios.post('/api/auth/user/close',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "关闭成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
    }
}